package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.dto.HandleQuestionFeedbackDto;
import com.ruoyi.system.dto.QuestionFeedbackAddDto;
import com.ruoyi.system.mapper.QuestionFeedbackGenMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.QuestionFeedbackService;
import com.ruoyi.system.utils.BeanUtil;
import com.ruoyi.system.utils.IdGenerator;
import com.ruoyi.system.domain.QuestionFeedbackGen;
import com.ruoyi.system.domain.QuestionFeedbackGenExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class QuestionFeedbackServiceImpl implements QuestionFeedbackService {
    @Resource
    private QuestionFeedbackGenMapper questionFeedbackGenMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public boolean addQuestionFeedbackAdd(QuestionFeedbackAddDto dto) {
        QuestionFeedbackGen questionFeedbackGen = new QuestionFeedbackGen();
        BeanUtil.copyProperties(dto,questionFeedbackGen);
        questionFeedbackGen.setFeedbackCode(IdGenerator.generateId());
        questionFeedbackGen.setAliveFlag(1);
        questionFeedbackGen.setCommitTime(DateUtils.getNowDate());
        questionFeedbackGenMapper.insertSelective(questionFeedbackGen);
        return true;
    }

    @Override
    public PageInfo<QuestionFeedbackGen> queryQuestionFeedbackBycondition(QuestionFeedbackAddDto dto) {

        // 若仅传入 commitTime（某一天），自动计算当天起止时间
        if (dto.getCommitTime() != null && dto.getCommitTimeStart() == null && dto.getCommitTimeEnd() == null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dto.getCommitTime());
            cal.set(java.util.Calendar.HOUR_OF_DAY, 0);
            cal.set(java.util.Calendar.MINUTE, 0);
            cal.set(java.util.Calendar.SECOND, 0);
            cal.set(java.util.Calendar.MILLISECOND, 0);
            Date dayStart = cal.getTime();
            cal.set(java.util.Calendar.HOUR_OF_DAY, 23);
            cal.set(java.util.Calendar.MINUTE, 59);
            cal.set(java.util.Calendar.SECOND, 59);
            cal.set(java.util.Calendar.MILLISECOND, 999);
            Date dayEnd = cal.getTime();
            dto.setCommitTimeStart(dayStart);
            dto.setCommitTimeEnd(dayEnd);
        }
        QuestionFeedbackGenExample questionFeedbackGenExample = new QuestionFeedbackGenExample();
        QuestionFeedbackGenExample.Criteria criteria = questionFeedbackGenExample.createCriteria();
        criteria.andAliveFlagEqualTo(1);
        if (!ObjectUtils.isEmpty(dto.getFeedbackType())){
            criteria.andFeedbackTypeEqualTo(dto.getFeedbackType());
        }
        if (!ObjectUtils.isEmpty(dto.getStatus())){
            criteria.andStatusEqualTo(dto.getStatus());
        }
        if (!ObjectUtils.isEmpty(dto.getCommitTimeStart())){
            criteria.andCommitTimeGreaterThanOrEqualTo(dto.getCommitTimeStart());
        }
        if (!ObjectUtils.isEmpty(dto.getCommitTimeEnd())){
            criteria.andCommitTimeLessThanOrEqualTo(dto.getCommitTimeEnd());
        }
        if(!ObjectUtils.isEmpty(dto.getUserPhoneNumber())){
            criteria.andUserPhoneNumberLike("%"+dto.getUserPhoneNumber()+"%");
        }
        // 排序：按提交时间倒序
        questionFeedbackGenExample.setOrderByClause("commit_time desc");
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<QuestionFeedbackGen> questionFeedbackGens = questionFeedbackGenMapper.selectByExample(questionFeedbackGenExample);

        return new PageInfo<>(questionFeedbackGens);
    }

    @Override
    public boolean handleQuestionFeedback(HandleQuestionFeedbackDto dto) {
        QuestionFeedbackGen questionFeedbackGen = new QuestionFeedbackGen();
        questionFeedbackGen.setFeedbackId(dto.getFeedbackId());
        questionFeedbackGen.setStatus(dto.getStatus());
        questionFeedbackGen.setHandlingOpinion(dto.getHandlingOpinion());
        questionFeedbackGen.setHandlingTime(DateUtils.getNowDate());
        questionFeedbackGen.setHandlingDetail(dto.getHandlingDetail());
        questionFeedbackGen.setHandlingUserid(dto.getHandlingUserid());
        SysUser sysUser = sysUserMapper.selectUserById(dto.getHandlingUserid());
        if (!ObjectUtils.isEmpty(sysUser) && !ObjectUtils.isEmpty(sysUser.getNickName())){
            questionFeedbackGen.setHandlingUserName(sysUser.getNickName());
        }
        questionFeedbackGenMapper.updateByPrimaryKeySelective(questionFeedbackGen);
        return true;
    }

    @Override
    public boolean closeQuestionFeedback(Long id) {
        QuestionFeedbackGen questionFeedbackGen = new QuestionFeedbackGen();
        questionFeedbackGen.setFeedbackId(id);
        questionFeedbackGen.setStatus("03");
        questionFeedbackGenMapper.updateByPrimaryKeySelective(questionFeedbackGen);
        return true;
    }

}
